package cn.it.shop.dao.impl;

import java.util.List;

import org.hibernate.classic.Session;
import org.springframework.stereotype.Repository;

import cn.it.shop.dao.CategoryDao;
import cn.it.shop.pojo.Category;

@SuppressWarnings("unchecked")
@Repository(value = "categoryDao")
public class CategoryDaoImpl extends BaseDaoImpl<Category> implements
		CategoryDao {

	public CategoryDaoImpl() {
		super();
	}

	@Override
	public List<Category> queryFetchAccount(String type) {
		// FETCH: account对象抓取出来, 然后存放到Category,如果没有此关键字返回的是Object[]
		String hql = "FROM Category c LEFT JOIN FETCH c.account WHERE c.type like :type";
		Session session = sessionFactory.getCurrentSession();
		return session.createQuery(hql)//
				.setString("type", "%" + type + "%")//
				.list();//
	}
	
	

	@Override
	public void update(Category model) {
		String hql="UPDATE FROM Category c SET c.type=:type,c.hot=:hot WHERE c.id=:id";
		Session session = sessionFactory.getCurrentSession();
		session.createQuery(hql) //
		.setString("type",model.getType()) //
		.setBoolean("hot",model.getHot()) //
		.setInteger("id", model.getId()) //
		.executeUpdate();
	}

	@Override
	public List<Category> queryByHot(boolean isHot) {
		String hql = "FROM Category c WHERE c.hot=:hot";
		Session session = sessionFactory.getCurrentSession();
		return session.createQuery(hql)//
				.setBoolean("hot", isHot)//
				.setFirstResult(0)//
				.setMaxResults(4)//
				.list();
	}
}
